package m.jclecai.com.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import m.jclecai.com.R;

import java.util.HashMap;
import java.util.Map;

import m.jclecai.com.adapter.RecyclerAdapter;
import m.jclecai.com.listener.ISelectPlayListener;
import m.jclecai.com.utils.StringUtils;

/**
 * 自由过关
 * A simple {@link Fragment} subclass.
 */
public class TZPOPFragment extends Fragment {
    private ISelectPlayListener listener;
    private String value = "";
    //选择彩票的总数
    private int itemSize = 0,minCount = 0;
    private boolean isDG = false;

    Map<String, String> map;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recyler_hemai, container, false);
        if(map == null)
            map = new HashMap<>();
        //如果有初始值 那么就不在设置那个最大的默认值了
        String[] types = getResources().getStringArray(R.array.lottery_pass_types_list);
        types = StringUtils.filterData(types,"[*]",itemSize,minCount,isDG);
        String[] names = getResources().getStringArray(R.array.lottery_pass_types_name_list);
        names = StringUtils.filterData(names,"串",itemSize,minCount,isDG);

        RecyclerView recycler_view = (RecyclerView) view.findViewById(R.id.recycler_view);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(gridLayoutManager);
        final RecyclerAdapter recyclerAdapter = new RecyclerAdapter(getActivity(), names, types, map);
        recycler_view.setAdapter(recyclerAdapter);
        recyclerAdapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(TextView v, int position) {
                String key = v.getText().toString();
                String tag = v.getTag().toString();
                if (map.containsKey(key)) {
                    map.remove(key);
                    v.setBackgroundResource(R.drawable.hemai_popuwindow_txt_bg);
                } else {
                    map.put(key, tag);
                    v.setBackgroundResource(R.drawable.hemai_popuwindow_txt_bg_press);
                }
            }
        });

        return view;
    }


    public String getData(){
        value = "";
        for (String str:map.keySet()){
            if(TextUtils.isEmpty(str))continue;
            value += str+",";
        }
        if(value.length()>0)
            value = value.substring(0,value.length()-1);
        if(TextUtils.isEmpty(value)) return "";
        return value;
    }

    public void setDG(boolean isDG){
        this.isDG = isDG;
    }

    public void setItemCount(int count){
        this.itemSize = count;
    }

    boolean check = false;
    public void setDefCheck(boolean check,String value){
        this.check=check;
        this.value = value;
        String[] split = value.split(",");
        if(map == null)
            map = new HashMap<>();
        for (int i=0;i<split.length;i++){
            map.put(split[i].replaceAll("[*]","串"),split[i]);
        }
    }


    public void setMinCount(int minCount) {
        this.minCount = minCount;
    }
}
